﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kay.LazarToDo.Win.AppServices;
using Kay.LazarToDo.Win.Common;
using Kay.LazarToDo.Win._Controls;

namespace Kay.LazarToDo.Win._Forms
{
    /// <summary>
    /// 项目列表
    /// </summary>
    public partial class ProjectList : Form
    {
        private readonly ProjectAppService _projectAppService;

        /// <summary>
        /// 初始化项目列表
        /// </summary>
        public ProjectList()
        {
            InitializeComponent();
            _projectAppService = new ProjectAppService();
        }

        private void ProjectList_Load(object sender, EventArgs e)
        {
            Application.DoEvents();
            RefreshData();
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshData()
        {
            GC.Collect();
            var data = _projectAppService.GetAllList();
            foreach (var project in data)
            {
                GlobalVariables.DbContext.Entry(project).Collection(p => p.LazarToDoCollection).Load();
                var projectDetails = new ProjectDetails
                {
                    ProjectName = $"{project.Name} | {project.CreationTime:yyyy/MM/dd}",
                    Shown = project.Shown,
                    ToDoCount = project.LazarToDoCollection?.Count ?? 0,
                    Project = project
                };
                projectDetails.RemoveComplated += OnProjectDetailsRemoveComplated;
                projectDetails.UpdateComplated += (sender, e) =>
                {
                    ClearProject();
                    RefreshData();
                };
                flowLayoutPanelProjectList.Controls.Add(projectDetails);
            }
        }

        private void OnProjectDetailsRemoveComplated(object sender, EventArgs e)
        {
            var projectDetails = (ProjectDetails)sender;
            flowLayoutPanelProjectList.Controls.Remove(projectDetails);
            projectDetails.Dispose();
            GC.Collect();
        }

        private void linkLabelAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (var frm = new AddProject())
            {
                var result = frm.ShowDialog();
                if (result == DialogResult.OK)
                {
                    ClearProject();
                    RefreshData();
                }
            }
            GC.Collect();
        }

        /// <summary>
        /// 清楚所有项目
        /// </summary>
        public void ClearProject()
        {
            flowLayoutPanelProjectList.SuspendLayout();
            foreach (Control control in flowLayoutPanelProjectList.Controls)
            {
                control.Dispose();
                GC.Collect();
            }
            flowLayoutPanelProjectList.Controls.Clear();
            GC.Collect();
            flowLayoutPanelProjectList.ResumeLayout();
        }
    }
}
